package ihm.tfidf;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.VariableStatiques;
import weka.core.Instances;
import weka.gui.AttributeSelectionPanel;
import weka.gui.arffviewer.ArffPanel;

public class TfIdfRight extends JPanel implements ActionListener {

	private JButton genererMatrice;
	private JPanel buttonPanel;
	private Instances instanceTfIdf;
	private ArffPanel panelMatrice;
	private HashMap<Integer, String> mapFiltre = new HashMap<Integer, String>();
	private AttributeSelectionPanel alp;
	private JButton btnFilter;
	private JButton btnReset;
	private JTextField textChoixInstance;


	public TfIdfRight(){
		setLayout(new BorderLayout());
		createButtonPanel();
		add(buttonPanel, BorderLayout.WEST);
	} 


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == genererMatrice){
			createInstances();
			if (panelMatrice != null){
				remove(panelMatrice);
				remove(alp);
			}
			createMatrice();
			createListeMots();
		}
		if (e.getSource() == btnFilter){
			filtrer(alp.getSelectedAttributes());
		}
		if (e.getSource() == btnReset){
			createInstances();
			if (panelMatrice != null){
				remove(panelMatrice);
				remove(alp);
			}
			createMatrice();
			createListeMots();
		} 

	} 


	public void createInstances(){
		try {
			if (textChoixInstance.getText() == null || textChoixInstance.getText().equals("")){
				instanceTfIdf = VariableStatiques.STRING_TO_VECTOR_MATRICE.outputFilter(VariableStatiques.FILTER_INSTANCES);
			}
			else{
				VariableStatiques.STRING_TO_VECTOR_MATRICE.setWordsToKeep(Integer.valueOf(textChoixInstance.getText()));
				VariableStatiques.STRING_TO_VECTOR_MATRICE.setInputFormat();
				instanceTfIdf = VariableStatiques.STRING_TO_VECTOR_MATRICE.outputFilter(VariableStatiques.FILTER_INSTANCES);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}

	}


	public void createMatrice(){
		panelMatrice = new ArffPanel(instanceTfIdf);
		for (int i = 0; i<instanceTfIdf.numAttributes(); ++i){
			mapFiltre.put(i, (String)panelMatrice.getTable().getColumnModel().getColumn(i).getHeaderValue());
		}
		add(panelMatrice, BorderLayout.CENTER);
		this.validate();
	} 

	public void createListeMots(){
		alp = new AttributeSelectionPanel(false, false, false, false);
		alp.setInstances(instanceTfIdf);
		add(alp, BorderLayout.EAST);
		this.validate();
	} 


	public void createButtonPanel(){
		buttonPanel = new JPanel();
		genererMatrice = new JButton("Créer MATRICE");
		genererMatrice.setSize(50, 25);
		btnFilter = new JButton("Filtrer CONTENU");
		btnReset = new JButton("tout DECOCHER");
		JLabel lblField = new JLabel("NB mots à afficher");
		textChoixInstance = new JTextField(Integer.toString(VariableStatiques.STRING_TO_VECTOR_MATRICE.getWordsToKeep()));
		btnFilter.addActionListener(this);
		btnReset.addActionListener(this);
		genererMatrice.addActionListener(this);
		buttonPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		buttonPanel.add(lblField, c);
		c.gridy = 1;
		buttonPanel.add(textChoixInstance,c);
		c.gridx=0;
		c.gridy = 2;
		buttonPanel.add(genererMatrice,c);
		c.gridy = 3;
		buttonPanel.add(btnFilter,c);
		c.gridy = 4;
		buttonPanel.add(btnReset,c);
		buttonPanel.validate();
	} 

	public JButton getGenererMatrice() {
		return genererMatrice;
	}
	public void setGenererMatrice(JButton genererMatrice) {
		this.genererMatrice = genererMatrice;
	}
	public void filtrer(int[] listeFiltres){
		for (int i : listeFiltres){
			String entete = mapFiltre.get(i+1);
			for (int j = 0; j < panelMatrice.getTable().getColumnCount(); ++j){
				if (panelMatrice.getTable().getColumnModel().getColumn(j).getHeaderValue().equals(entete)){
					panelMatrice.getTable().removeColumn(panelMatrice.getTable().getColumnModel().getColumn(j));
				}
			}
		}
		panelMatrice.validate();
	}
	public static void reset() {
	} 



}
